package com.tansun.easycare.rule.funcparam.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.tansun.easycare.core.utils.ResponseTable;
import com.tansun.easycare.rule.codeblock.entity.RuleCodeblock;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparams;
import com.tansun.easycare.rule.datamodel.entity.RuleDataparamsInfo;
import com.tansun.easycare.rule.datamodel.entity.RuleDataset;
import com.tansun.easycare.rule.datamodel.service.RuleDataparamsService;
import com.tansun.easycare.rule.datamodel.service.impl.DataModelServiceImpl;
import com.tansun.rule.utils.StringUtils;

/**
 * 函数变量controller  ljt 20200203
 */
@RestController
@RequestMapping(value = "/ruleProj/funcparam")
public class RuleFuncparamController extends RuleBaseController {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    @Autowired
    private RuleDataparamsService ruleDataparamsService;
    @Autowired
    private DataModelServiceImpl dataModelService;


    /**
     * 进去到函数变量管理页面
     * @param projectCode
     * @param modelMap
     * @return
     */
    @RequestMapping("index")
    public ResponseTable<RuleDataparamsInfo> index(@RequestBody RuleDataparamsInfo ruleDataparamsInfo) throws Exception {
    	PageInfo<RuleDataparamsInfo>   pageInfo = this.ruleDataparamsService.findFuncParamPage(ruleDataparamsInfo.getPage(), ruleDataparamsInfo);
    	ResponseTable<RuleDataparamsInfo>  resVo  = new  ResponseTable<RuleDataparamsInfo>(pageInfo);
         
    	//查询出是否存在func的数据集(初始化函数变量对应的数据集)
        RuleDataset condiction = new RuleDataset();
        condiction.setName("func");
        condiction.setChzn("函数变量");
        condiction.setDeleteflag("1");
        condiction.setProjectcode(ruleDataparamsInfo.getProjectcode());
        List<RuleDataset> rds = this.dataModelService.findList(condiction);
        if(rds!=null&&rds.size()>0){
        	//modelMap.put("dataset",rds.get(0));
     	  List<RuleDataparamsInfo> funcpList= pageInfo.getList();
     	  for(RuleDataparamsInfo ruleDataparamsInfo1 : funcpList) {
     		  if(ruleDataparamsInfo1.getDatasetName().isEmpty()) {
     			 ruleDataparamsInfo1.setDatasetName(rds.get(0).getName());
     		  }
     	  }
     	  
        }else{
            //创建一个数据模型进行挂靠
            RuleDataset dm = new RuleDataset();
            dm.setProjectcode(ruleDataparamsInfo.getProjectcode());
            dm.setName("func");
            dm.setChzn("函数变量");
            dm.setParamType("funcparam");
            RuleDataset rd = new RuleDataset();
            rd.setProjectcode(ruleDataparamsInfo.getProjectcode());
            List<RuleDataset> datasetList = this.dataModelService.queryRuleDataList(rd);
            validation(dm,datasetList);
            this.dataModelService.save(dm);
//            modelMap.put("dataset", dm);
            List<RuleDataparamsInfo> funcpList= pageInfo.getList();
            if(funcpList.size()>0) {
            	for(RuleDataparamsInfo ruleDataparamsInfo1 : funcpList) {
             		  if(ruleDataparamsInfo1.getDatasetName().isEmpty()) {
             			 ruleDataparamsInfo1.setDatasetName(dm.getName());
             		  }
             	  }
            	
            }
            
        }
    	return resVo;
    }
 
    private void validation(RuleDataset ruleDataset,List<RuleDataset> list){
        int i = 0;
        for (RuleDataset dataset : list) {
           if(ruleDataset.getName().equals(dataset.getName())){
               ruleDataset.setName("dataset"+sdf.format(new Date()));
               i = 1;
               break;
           }
        }
        if(i==1){
            validation(ruleDataset,list);
        }
    }
    
    /**
     *	 根据projectcode和chzn查找该项目的函数变量对应的数据集信息
     *
     * @param  ruleDataset
     * @return 根据projectcode和chzn查找该项目的函数变量对应的数据集信息
     */    
    @RequestMapping("funcDataset")
    public @ResponseBody ResponseVO funcDataset(@RequestBody RuleDataset ruleDataset) throws Exception {
    	ResponseVO rev = new ResponseVO();
    	try {
    		List<RuleDataset> list = this.dataModelService.getList(ruleDataset);
        	rev.setData(list);
    	}catch (Exception e) {
    		e.printStackTrace();
    		rev.setStatus(ResponseVO.ERROR);
    		rev.setDescription(e.getMessage());
			// TODO: handle exception
		}
        return rev;
    }
    
}
